package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.adapter.HoldLotListAdapter;
import com.glory.hzlionpda.base.BasePollingActivity;
import com.glory.hzlionpda.bean.HoldCodeBean;
import com.glory.hzlionpda.bean.HoldLotBean;
import com.glory.hzlionpda.bean.LotBean;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HoldActivity extends BasePollingActivity {


    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.ivTypeArrow)
    ImageView ivTypeArrow;
    @BindView(R.id.flType)
    FrameLayout flType;
    @BindView(R.id.tvCode)
    TextView tvCode;
    @BindView(R.id.ivCodeArrow)
    ImageView ivCodeArrow;
    @BindView(R.id.flCode)
    FrameLayout flCode;
    @BindView(R.id.rvLots)
    RecyclerView rvLots;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.cbCheckAll)
    CheckBox cbCheckAll;

    private LotBean currentLotBean;
    private HoldLotListAdapter chooseWaferListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_hold;
    }

    @Override
    protected void initView() {
        rvLots.setLayoutManager(new LinearLayoutManager(this));
        rvLots.setItemAnimator(new DefaultItemAnimator());
        rvLots.addItemDecoration(new DividerItemDecoration(this, 1));
    }

    @Override
    protected void initData() {
        currentLotBean = (LotBean) getIntent().getSerializableExtra("currentLotBean");
        List<HoldLotBean> holdBeans = new ArrayList<>();
        for (String str : currentLotBean.getJOBLOTLIST()) {
            HoldLotBean holdBean = new HoldLotBean();
            holdBean.setLotId(str);
            holdBean.setSelect(false);
            holdBeans.add(holdBean);
        }
        chooseWaferListAdapter = new HoldLotListAdapter(holdBeans);
        chooseWaferListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        chooseWaferListAdapter.isFirstOnly(true);
        rvLots.setAdapter(chooseWaferListAdapter);
        cbCheckAll.setOnCheckedChangeListener((compoundButton, isChecked) -> chooseWaferListAdapter.setAll(isChecked));
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("暂停批次");
        freeUI_titleView.setRightListener("确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String type = tvType.getText().toString();
                if (FreeApi_StringUtils.isEmpty(type)) {
                    showToast("请选择HOLD类型", false);
                    return;
                }
                String code = tvCode.getText().toString();
                if (FreeApi_StringUtils.isEmpty(code)) {
                    showToast("请选择暂停代码", false);
                    return;
                }
                StringBuffer sb = new StringBuffer();
                for (HoldLotBean datum : chooseWaferListAdapter.getData()) {
                    if (datum.isSelect()) {
                        sb.append(datum.getLotId());
                        sb.append(",");
                    }
                }
                if (FreeApi_StringUtils.isEmpty(sb.toString())) {
                    showToast("请选择LotId", false);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("type", type);
                intent.putExtra("code", code);
                intent.putExtra("lots", sb.toString().substring(0, sb.toString().length() - 1));
                setResult(2102, intent);
                HoldActivity.this.finish();
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }


    @OnClick({R.id.flType, R.id.flCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flType:
                FreeUI_GeneralPop typePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flType.getWidth(),
                        currentLotBean.getHOLDTYPE(), ivTypeArrow, (s, i) -> {
                            tvType.setText(s);
                        });
                if (typePop.isShowing()) {
                    typePop.dismiss();
                } else {
                    FreeApi_PopUtils.setArrowShow(ivTypeArrow);
                    typePop.showAsDropDown(flType, 0, 0);
                }
                break;
            case R.id.flCode:
                List<String> codeList = new ArrayList<>();
                for (HoldCodeBean holdCodeBean : currentLotBean.getHOLDCODE()) {
                    codeList.add(holdCodeBean.getCODEVALUE());
                }
                FreeUI_GeneralPop codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flCode.getWidth(),
                        codeList, ivCodeArrow, (s, i) -> {
                            tvCode.setText(s);
                        });
                if (codePop.isShowing()) {
                    codePop.dismiss();
                } else {
                    FreeApi_PopUtils.setArrowShow(ivCodeArrow);
                    codePop.showAsDropDown(flCode, 0, 0);
                }
                break;
        }
    }
}

